#include "reminderdialog.h"
#include "ui_reminderdialog.h"

#include <QVBoxLayout>
#include <qpushbutton.h>
#include <QKeyEvent>
#include <QApplication>
#include <QDesktopWidget>
#include <QScreen>
#include <QTimer>
#include <QLineEdit>
#include <QMessageBox>
#include <QHBoxLayout>
#include <QPropertyAnimation>

// 初始化静态成员变量
QString ReminderDialog::s_password = "123456"; // 默认密码

ReminderDialog::ReminderDialog(const QString &message, int countdownSeconds, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ReminderDialog),
    m_countdownTimer(new QTimer(this)),
    m_remainingSeconds(countdownSeconds)
{
    ui->setupUi(this);

    // 设置窗口标志：无边框、置顶、任务栏显示
    setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Tool);
    
    // 设置为全屏模式
    setWindowState(Qt::WindowFullScreen);
    
    // 设置背景色和字体颜色
    setStyleSheet("background-color: #B0E0E6; color: white;");
    
    // 强制获取焦点和置顶
    setAttribute(Qt::WA_ShowWithoutActivating, false);
    setModal(true);
    
    setupUI(message);
    
    // 连接倒计时定时器
    connect(m_countdownTimer, &QTimer::timeout, this, &ReminderDialog::updateCountdown);
    
    // 开始倒计时
    startCountdown(countdownSeconds);
    
    // 使用定时器确保窗口真正置顶和获得焦点
    QTimer::singleShot(100, this, [this]() {
        show();
        raise();
        activateWindow();
        setFocus();
        
        // 在Windows上强制置顶
#ifdef Q_OS_WIN
        SetWindowPos((HWND)winId(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
#endif
    });
}

void ReminderDialog::setupUI(const QString &message)
{
    QVBoxLayout *layout = new QVBoxLayout(this);

    m_messageLabel = new QLabel(message, this);
    m_messageLabel->setAlignment(Qt::AlignCenter);
    m_messageLabel->setWordWrap(true); // 允许长文本自动换行
    m_messageLabel->setStyleSheet("font-size: 48px; font-weight: bold; margin: 50px; font-family: 'Microsoft YaHei UI', 'Segoe UI', Arial, sans-serif;");

    // 添加倒计时标签
    m_countdownLabel = new QLabel(this);
    m_countdownLabel->setAlignment(Qt::AlignCenter);
    m_countdownLabel->setStyleSheet("font-size: 36px; font-weight: bold; margin: 20px; color: #FFD700; background-color: rgba(255, 255, 255, 0.1); border-radius: 10px; padding: 15px; font-family: 'Microsoft YaHei UI', 'Segoe UI', Arial, sans-serif;");

    // 添加密码输入区域
    m_passwordLabel = new QLabel("请输入密码关闭提醒:", this);
    m_passwordLabel->setAlignment(Qt::AlignCenter);
    m_passwordLabel->setStyleSheet("font-size: 24px; font-weight: bold; margin: 20px; font-family: 'Microsoft YaHei UI', 'Segoe UI', Arial, sans-serif;");
    
    m_passwordEdit = new QLineEdit(this);
    m_passwordEdit->setEchoMode(QLineEdit::Password);
    m_passwordEdit->setStyleSheet("font-size: 20px; padding: 10px; background-color: white; color: black; border: 2px solid #2E8B57; border-radius: 5px; font-family: 'Microsoft YaHei UI', 'Segoe UI', Arial, sans-serif;");
    m_passwordEdit->setMaximumWidth(300);
    m_passwordEdit->setAlignment(Qt::AlignCenter);
    m_passwordEdit->setEnabled(false); // 初始禁用密码输入
    
    // 连接回车键到验证密码
    connect(m_passwordEdit, &QLineEdit::returnPressed, this, &ReminderDialog::validatePassword);

    m_closeButton = new QPushButton("确认关闭", this);
    m_closeButton->setStyleSheet("font-size: 28px; padding: 15px; background-color: #B0E0E6; color: #2E8B57; font-family: 'Microsoft YaHei UI', 'Segoe UI', Arial, sans-serif;");
    m_closeButton->setMinimumSize(200, 80);
    m_closeButton->setEnabled(false); // 初始禁用关闭按钮

    // 连接按钮的点击信号到验证密码槽函数
    connect(m_closeButton, &QPushButton::clicked, this, &ReminderDialog::validatePassword);

    layout->addWidget(m_messageLabel);
    layout->addWidget(m_countdownLabel);
    layout->addWidget(m_passwordLabel);
    
    // 创建水平布局来居中密码输入框
    QHBoxLayout *passwordLayout = new QHBoxLayout();
    passwordLayout->addStretch();
    passwordLayout->addWidget(m_passwordEdit);
    passwordLayout->addStretch();
    
    layout->addLayout(passwordLayout);
    layout->addWidget(m_closeButton, 0, Qt::AlignCenter);

    setLayout(layout);
    
    // 添加背景闪烁效果来吸引注意力
    QTimer *blinkTimer = new QTimer(this);
    connect(blinkTimer, &QTimer::timeout, this, [this]() {
        static bool isOriginalColor = true;
        if (isOriginalColor) {
            // 切换到闪烁颜色（更亮的绿色）
            setStyleSheet("background-color: #87CEEB; color: white;");
        } else {
            // 恢复原始颜色
            setStyleSheet("background-color: #B0E0E6; color: white;");
        }
        isOriginalColor = !isOriginalColor;
    });
    blinkTimer->start(500); // 每500毫秒闪烁一次
    
    // 使用倒计时时间作为闪烁持续时间（转换为毫秒）
    QTimer::singleShot(m_remainingSeconds * 1000, this, [this, blinkTimer]() {
        blinkTimer->stop();
        setStyleSheet("background-color: #B0E0E6; color: white;");
    });
}

void ReminderDialog::keyPressEvent(QKeyEvent *event)
{
    // 只允许ESC键关闭，但需要验证密码
    if (event->key() == Qt::Key_Escape) {
        validatePassword();
    } else {
        QDialog::keyPressEvent(event);
    }
}

// 验证密码
void ReminderDialog::validatePassword()
{
    QString inputPassword = m_passwordEdit->text();
    
    if (inputPassword == s_password) {
        closeDialog();
    } else {
        QMessageBox::warning(this, "密码错误", "密码不正确，请重新输入！");
        m_passwordEdit->clear();
        m_passwordEdit->setFocus();
    }
}

void ReminderDialog::closeDialog()
{
    accept(); // 关闭对话框
}

// 静态方法实现
void ReminderDialog::setPassword(const QString &password)
{
    s_password = password;
}

QString ReminderDialog::getPassword()
{
    return s_password;
}

// 开始倒计时
void ReminderDialog::startCountdown(int seconds)
{
    m_remainingSeconds = seconds;
    updateCountdown(); // 立即更新显示
    m_countdownTimer->start(1000); // 每秒更新一次
}

// 更新倒计时显示
void ReminderDialog::updateCountdown()
{
    if (m_remainingSeconds > 0) {
        m_countdownLabel->setText(QString("倒计时: %1 秒").arg(m_remainingSeconds));
        m_remainingSeconds--;
    } else {
        // 倒计时结束，启用密码输入并美化界面
        m_countdownTimer->stop();
        
        // 创建美化的倒计时结束提示
        m_countdownLabel->setText("🎉 倒计时结束！现在可以输入密码了 🎉");
        
        // 美化倒计时标签样式 - 使用渐变背景和阴影效果
        m_countdownLabel->setStyleSheet(
            "font-size: 32px; "
            "font-weight: bold; "
            "margin: 20px; "
            "color: #FFFFFF; "
            "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, "
                "stop:0 #87CEEB, stop:0.5 #87CED0, stop:1 #87CEC0); "
            "border: 3px solid #FFD700; "
            "border-radius: 15px; "
            "padding: 20px; "
            "font-family: 'Microsoft YaHei UI', 'Segoe UI', Arial, sans-serif; "
            "text-shadow: 2px 2px 4px rgba(0,0,0,0.5);"
        );
        
        // 美化密码输入框
        m_passwordEdit->setStyleSheet(
            "font-size: 22px; "
            "padding: 15px; "
            "background: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
                "stop:0 #F0F8FF, stop:1 #E6F3FF); "
            "color: #2E8B57; "
            "border: 3px solid #32CD32; "
            "border-radius: 10px; "
            "font-family: 'Microsoft YaHei UI', 'Segoe UI', Arial, sans-serif; "
            "selection-background-color: #90EE90;"
        );
        
        // 美化关闭按钮
        m_closeButton->setStyleSheet(
            "QPushButton {"
                "font-size: 24px; "
                "padding: 18px 30px; "
                "background: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
                    "stop:0 #98FB98, stop:0.5 #90EE90, stop:1 #7CFC00); "
                "color: #2E8B57; "
                "border: 2px solid #32CD32; "
                "border-radius: 12px; "
                "font-weight: bold; "
                "font-family: 'Microsoft YaHei UI', 'Segoe UI', Arial, sans-serif; "
                "text-shadow: 1px 1px 2px rgba(255,255,255,0.8);"
            "} "
            "QPushButton:hover {"
                "background: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
                    "stop:0 #ADFF2F, stop:0.5 #9AFF9A, stop:1 #90EE90); "
                "border: 2px solid #228B22; "
                "transform: scale(1.05);"
            "} "
            "QPushButton:pressed {"
                "background: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
                    "stop:0 #7CFC00, stop:0.5 #90EE90, stop:1 #98FB98); "
                "border: 2px solid #006400;"
            "}"
        );
        
        // 美化密码标签
        m_passwordLabel->setStyleSheet(
            "font-size: 26px; "
            "font-weight: bold; "
            "margin: 15px; "
            "color: #2E8B57; "
            "background-color: rgba(144, 238, 144, 0.3); "
            "border-radius: 8px; "
            "padding: 10px; "
            "font-family: 'Microsoft YaHei UI', 'Segoe UI', Arial, sans-serif; "
            "text-shadow: 1px 1px 2px rgba(255,255,255,0.8);"
        );
        
        m_passwordEdit->setEnabled(true);
        m_closeButton->setEnabled(true);
        m_passwordEdit->setFocus();
        
        // 添加一个简单的"呼吸"动画效果给密码输入框
        QPropertyAnimation *animation = new QPropertyAnimation(m_passwordEdit, "geometry");
        animation->setDuration(1000);
        animation->setLoopCount(3);
        QRect originalGeometry = m_passwordEdit->geometry();
        QRect expandedGeometry = originalGeometry.adjusted(-5, -2, 5, 2);
        animation->setKeyValueAt(0, originalGeometry);
        animation->setKeyValueAt(0.5, expandedGeometry);
        animation->setKeyValueAt(1, originalGeometry);
        animation->start(QAbstractAnimation::DeleteWhenStopped);
    }
}

ReminderDialog::~ReminderDialog()
{
    delete ui;
}
